package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.constant.QiniuConstant;
import cn.huiyunche.base.service.framework.utils.HYCUtils;
import cn.huiyunche.base.service.framework.utils.QiniuUtils;
import cn.huiyunche.base.service.interfaces.BankService;
import cn.huiyunche.base.service.mappers.BBankMapper;
import cn.huiyunche.base.service.model.BBank;
import cn.huiyunche.base.service.vo.OutBankVo;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LYN on 8/2/16.
 */
@Service
public class BankServiceImpl implements BankService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BankServiceImpl.class);

    @Autowired
    private BBankMapper bankMapper = null;

    @Override
    public void add(BBank bankModel) {
        LOGGER.info("add param : {}", bankModel);
        bankModel.setName("Common");
        this.getBankMapper().insertSelective(bankModel);
        try {
            bankModel.setName("Exception");
            this.getBankMapper().insertSelective(bankModel);
            throw new BusinessException("Error");
        } catch (Exception e) {
            throw new BusinessException(e.getMessage());
        }
    }

    @Override
    public List<OutBankVo> list() {
        List<OutBankVo> out = new ArrayList<>();
        List<BBank> list = this.bankMapper.selectByExample(null);
        String bankLogoBucket = HYCUtils.getConstantPropertiesLoader().getProperty(QiniuConstant.QINIU_BANK_LOGO_BUCKET);
        for (BBank bb : list) {
            out.add(new OutBankVo(bb.getId().toString(), bb.getName(),
                    QiniuUtils.generateDownloadTicket(bankLogoBucket, bb.getLogo(), "", "", ""),
                    QiniuUtils.generateDownloadTicket(bankLogoBucket, bb.getLogoSecond(), "", "", ""),
                    QiniuUtils.generateDownloadTicket(bankLogoBucket, bb.getBackgroundLogo(), "", "", ""),
                    bb.getLogoColor()));
        }
        return out;
    }

    public BBankMapper getBankMapper() {
        return bankMapper;
    }
}
